home *** CD-ROM | disk | FTP | other *** search
Text File | 1991-11-10 | 8.3 KB | 390 lines | [TEXT/PJMM] |
- unit RuleBook;
-
- { Generic Rule Book © Peter Lewis, Oct 1991 }
- { This program and its source are (is? who knows) Povertyware }
-
- interface
-
- uses
- QuickDrawRules, GameTypes;
-
- procedure Main (var ger: gameEventRecord);
-
- implementation
-
- const
- game_dlog = 1000; { The DLOG resource specifying the window size }
- game_dialog_item = 1; { The base Dialog Item used to do drawing }
-
- { Game Specific constants and types: }
- const
- rect_max = 4; { Number of rectangles to click in }
- game_status_item = 2; { The status item }
- status_click = 1;
- status_you = 2;
- status_waiting = 3;
- game_strh = 1000;
-
- type
- rects = 1..rect_max;
-
- type
- globalsRecord = record
- item_rect: rect;
- { Game Specific global variables: }
- therects: array[rects] of rect;
- end;
- globalsPeek = ^globalsRecord;
- gameRecord = record
- globals: globalsPeek;
- playing: boolean;
- connected: boolean;
- { Game Specific game variables: }
- state: rects;
- myturn: boolean;
- end;
- gamePeek = ^gameRecord;
-
- {Game Specific drawing routines:}
-
- procedure DrawTheRect (ggame: gamePeek; i: rects);
- var
- r: rect;
- begin
- r := ggame^.globals^.therects[i];
- FrameRect(r);
- InsetRect(r, 1, 1);
- if i = ggame^.state then
- FillRect(r, QDGlobals^.grey)
- else
- EraseRect(r);
- end;
-
- procedure DrawGame (gwindow: windowPtr; ggame: gamePeek; gglobals: globalsPeek);
- { Draw the game inside item_rect }
- var
- i: rects;
- begin
- for i := 1 to 4 do
- DrawTheRect(ggame, i);
- end;
-
- procedure DrawGameProc (wp: windowPtr; item: integer);
- { Touch Not: This is the update proc for game_dialog_item }
- var
- h: handle;
- begin
- h := handle(GetWRefCon(wp));
- HLock(h);
- DrawGame(wp, gamePeek(h^), gamePeek(h^)^.globals);
- HUnlock(h);
- end;
-
- procedure Main (var ger: gameEventRecord);
- var
- gwindow: windowPtr;
- ghandle: handle;
- ggame: gamePeek;
- gglobals: globalsPeek;
-
- procedure RedrawGame;
- begin
- DrawGame(gwindow, ggame, gglobals);
- end;
-
- {Game Specific routines:}
-
- procedure SetItemText (dlg: dialogPtr; item: integer; text: str255);
- var
- it: integer;
- ih: handle;
- box: rect;
- oldtext: str255;
- begin
- GetDItem(dlg, item, it, ih, box);
- GetIText(ih, oldtext);
- if oldtext <> text then
- SetIText(ih, text);
- end;
-
- procedure SetMyTurn;
- { Set ger.myturn, and fix up any displayed controls (eg White's Turn staus messages) }
- procedure SetStatus (index: integer);
- var
- s: str255;
- begin
- GetIndString(s, game_strh, index);
- SetItemText(gwindow, game_status_item, s);
- end;
- begin
- ger.myturn := ggame^.playing and ggame^.myturn;
- if ggame^.playing then
- if ggame^.connected then
- if ggame^.myturn then
- SetStatus(status_you)
- else
- SetStatus(status_waiting)
- else
- SetStatus(status_click);
- end;
-
- procedure DoMove (themove: integer);
- { Change parameters to define the move, and do the indicated move }
- { Call this in response to your player's moves and in responce to HandleMessage moves }
- var
- old: rects;
- begin
- ger.modified := true;
- old := ggame^.state;
- ggame^.state := themove;
- DrawTheRect(ggame, old);
- DrawTheRect(ggame, themove);
- SetMyTurn;
- end;
-
- procedure SendMove (themove: integer);
- { Change parameters to define the move, and send the message defined by the parameters }
- begin
- ger.event := ge_SendMessage;
- ger.message := concat('M', chr(themove));
- ggame^.myturn := false;
- SetMyTurn;
- end;
-
- procedure HandleMessage (s: str255);
- { Convert the string to a move and call DoMove }
- begin
- DoMove(ord(s[2]));
- ggame^.myturn := true;
- SetMyTurn;
- end;
-
- procedure DoClick (where: point);
- { Handle a user click, and turn it into a move, and then send it }
- var
- move: integer;
- i: rects;
- begin
- move := 0;
- with gglobals^ do
- for i := 1 to rect_max do
- if PtInRect(where, therects[i]) then
- move := i;
- if (move <> 0) and (move <> ggame^.state) then begin
- DoMove(move);
- if ggame^.connected then
- SendMove(move);
- end;
- end;
-
- procedure InitRuleBook (var r: rect);
- { Initialize the global variables, and return the size of the window }
- begin
- end;
-
- procedure FinishRuleBook;
- { Destroy the global variables you created }
- begin
- end;
-
- procedure InitGame;
- { Initialize the game variables, and global vars that require the window to be open }
- var
- i: rects;
- width, left: integer;
- begin
- with gglobals^ do begin
- { Set the rects from item_rect }
- for i := 1 to rect_max do
- therects[i] := item_rect;
- left := item_rect.left;
- width := item_rect.right - left;
- therects[1].right := left + width div 4;
- therects[2].right := left + width div 2;
- therects[3].right := left + 3 * width div 4;
- for i := 2 to rect_max do
- therects[i].left := therects[i - 1].right;
- end;
- ggame^.myturn := true;
- end;
-
- procedure RestartGame;
- { Reset all the vars in the ggame record }
- begin
- ggame^.state := 1;
- ger.modified := false;
- end;
-
- procedure Swap;
- { Swap the side you are playing (eg from White to Black). Don't swap which colour is to }
- { play. Thus, swap myturn. }
- begin
- ggame^.myturn := not ggame^.myturn;
- end;
-
- procedure ConnectionLost;
- { The connection has been broken }
- begin
- end;
-
- procedure ConnectionMade;
- { A new connection has been made }
- begin
- end;
-
- { Touch Not: Generic routines }
-
- procedure GInitRuleBook;
- { Touch Not: Creates gglobals }
- var
- r: rect;
- h: handle;
- begin
- ger.globals := handle(NewPtr(sizeof(globalsRecord)));
- gglobals := globalsPeek(ger.globals);
- h := Get1Resource('DLOG', game_dlog);
- if h = nil then
- SetRect(r, 0, 0, 100, 100)
- else begin
- BlockMove(h^, @r, SizeOf(rect));
- OffsetRect(r, -r.left, -r.top);
- end;
- InitRuleBook(r);
- with r do begin
- ger.int1 := left + right;
- ger.int2 := top + bottom;
- end;
- end;
-
- procedure GFinishRuleBook;
- { Touch Not: Destroy gglobals }
- begin
- FinishRuleBook;
- DisposPtr(ptr(ger.globals));
- ger.globals := nil;
- gglobals := nil;
- end;
-
- procedure GCommonInit;
- { Touch Not: Handle both New and Open game by reinitializing some game variables }
- var
- k: integer;
- h: handle;
- begin
- GetDItem(gwindow, game_dialog_item, k, h, gglobals^.item_rect);
- SetDItem(gwindow, game_dialog_item, k, handle(@DrawGameProc), gglobals^.item_rect);
- SetWRefCon(gwindow, longInt(ghandle));
- ggame^.globals := gglobals;
- ggame^.connected := false;
- InitGame;
- end;
-
- procedure GRestartGame;
- { Touch Not: Reset the game to the initial state. Don't change the side you are playing }
- begin
- RestartGame;
- SetMyTurn;
- RedrawGame;
- end;
-
- procedure GNewGame;
- { Touch Not: Set the ggame handle size, and initialize all the ggame variables. }
- begin
- HUnlock(ghandle);
- SetHandleSize(ghandle, SizeOf(gameRecord));
- HLock(ghandle);
- ggame := gamePeek(ghandle^);
- ggame^.playing := true;
- GCommonInit;
- GRestartGame;
- end;
-
- procedure GOldGame;
- { Touch Not: Initialize some of the ggame vars }
- begin
- GCommonInit;
- SetMyTurn;
- RedrawGame;
- end;
-
- procedure GSwap;
- { Touch Not: Swap which side you are playing }
- begin
- Swap;
- SetMyTurn;
- end;
-
- procedure GConnectionLost;
- { Touch Not: The connection has been broken }
- begin
- ggame^.connected := false;
- ConnectionLost;
- SetMyTurn;
- end;
-
- procedure GConnectionMade;
- { Touch Not: A new connection has been made }
- begin
- ggame^.connected := true;
- ConnectionMade;
- SetMyTurn;
- end;
-
- procedure GRestart;
- { Touch Not: Restart the game from scratch. Don't change which side you are playing }
- var
- r: rect;
- begin
- RestartGame;
- SetMyTurn;
- RedrawGame;
- end;
-
- procedure GMouseDown;
- { Touch Not: Handle clicks }
- begin
- DoClick(ger.where);
- end;
-
- procedure GMessageReceived;
- { Touch Not: Handle inbound messages }
- begin
- HandleMessage(ger.message);
- end;
-
- begin
- gglobals := globalsPeek(ger.globals);
- ghandle := ger.game;
- if ghandle <> nil then begin
- HLock(ghandle);
- ggame := gamePeek(ghandle^);
- end;
- GetPort(gwindow);
- case ger.event of
- ge_InitRuleBook:
- GInitRuleBook;
- ge_FinishRuleBook:
- GFinishRuleBook;
- ge_NewGame:
- GNewGame;
- ge_OldGame:
- GOldGame;
- ge_ConnectionLost:
- GConnectionLost;
- ge_ConnectionMade:
- GConnectionMade;
- ge_MessageReceived:
- GMessageReceived;
- ge_MouseDown:
- GMouseDown;
- ge_Swap:
- GSwap;
- ge_Restart:
- GRestart;
- otherwise
- end;
- if ghandle <> nil then
- HUnlock(ghandle);
- end;
-
- end.